package com.example.myapplication;

import static android.app.PendingIntent.getActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.myapplication.model.ChangePasswordTask;
import com.example.myapplication.model.SendMailTask;

public class PasswordActivity extends AppCompatActivity {
    private EditText newPassword;
    private EditText reNewPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("user", "Default");

        ImageView imageView_arrow = findViewById(R.id.imageView_arrow);
        imageView_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = sharedPreferences.getString("user", "Default");
                String url = "http://39.107.92.86:5000/change";
                newPassword = findViewById(R.id.editTextPhoneNumber);
                reNewPassword = findViewById(R.id.reditTextPhoneNumber);
                String newPasswordStr = newPassword.getText().toString();
                String reNewPasswordStr = reNewPassword.getText().toString();
                if(newPasswordStr.equals(reNewPasswordStr)) {
                    ChangePasswordTask task = new ChangePasswordTask(username, newPassword);
                    task.execute(url);
                }
                else {
                    Toast.makeText(PasswordActivity.this, "两次输入的密码不相同", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
